<?php

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                               |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//
namespace AuctionCore\LotNumber;

class LotNumber
{
    /**
     * 摇号文件列表
     *
     * @param $page
     * @param $page_size
     * @param $params
     *
     * @return array
     */
    public static function getLotNumberFileList($page, $page_size, $params)
    {
        $page = $page < 1 ? 1 : (int)$page;
        $page_size = $page_size < 1 ? 10 : (int)$page_size;
        $map = array();
        if ($params['title']) {
            $map['title'] = ['LIKE', '%' . $params['title'] . '%'];
        }
        if ($params['status']) {
            $map['status'] = ['eq', $params['status']];
        }
        if ($params['start_time']) {
            $map['create_time'] = ['egt', strtotime($params['start_time'] . ' 00:00:00')];
        }
        if ($params['end_time']) {
            $map['create_time'] = ['elt', strtotime($params['end_time'] . ' 23:59:59')];
        }
        if ($params['start_time'] && $params['end_time']) {
            $map['create_time'] = [
                ['egt', strtotime($params['start_time'] . ' 00:00:00')],
                ['elt', strtotime($params['end_time'] . ' 23:59:59')]
            ];
        }
        $cols = "*";
        $order = "id DESC";
        $total = M('lot_number_file', 'clcw_', 'DB_CLCW')->where($map)->count();
        $list = M('lot_number_file', 'clcw_', 'DB_CLCW')->field($cols)
            ->where($map)
            ->order($order)
            ->page($page)
            ->limit($page_size)
            ->select();
//        echo M('lot_number_file', 'clcw_', 'DB_CLCW')->getLastSql();
        if (!empty($list)) {
            foreach ($list as $k => $v) {
                $list[$k]['status_name'] = C('LOT_NUMBER_STATUS')[$v['status']];
                $list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
            }
        }
        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;
    }

    /**
     * 添加文件
     *
     * @param array $data
     *
     * @return bool|mixed
     */
    public function addLotNumberFile($data = [])
    {
        if (empty($data)) {
            return false;
        }
        $data['create_time'] = time();
        return M('lot_number_file', 'clcw_', 'DB_CLCW')->add($data);
    }

    /**
     * 修改文件
     *
     * @param array $data
     *
     * @return bool|mixed
     */
    public function editLotNumberFile($data = [])
    {
        if (empty($data) || empty($data['id'])) {
            return false;
        }
        $data['update_time'] = time();
        return M('lot_number_file', 'clcw_', 'DB_CLCW')->save($data);
    }


    /**
     * 获取文件信息
     *
     * @param int $id
     *
     * @return bool|mixed
     */
    public function getLotNumberFileById($id = 0)
    {
        if (empty($id)) {
            return false;
        }
        return M('lot_number_file', 'clcw_', 'DB_CLCW')->find($id);
    }

    /**
     * 获取文件信息
     *
     * @param array $map
     */
    public function getLotNumberFile($map = [])
    {
        if (empty($map)) {
            return false;
        }
        return M('lot_number_file', 'clcw_', 'DB_CLCW')->where($map)->find();
    }
}